package com.eelpo.cmmp.core.wicket.panel.navigator;

import com.eelpo.common.util.BlankUtils;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.AbstractLink;
import org.apache.wicket.markup.html.navigation.paging.IPageable;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigationIncrementLink;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigationLink;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.AbstractPageableView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.Model;

/**
 * 分页导航面板
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
public class PagingNavigator<T> extends Panel {

	private AbstractPageableView<T> pageable;
	private IModel<Integer> pageModel = new Model<Integer>();
	private static final long serialVersionUID = -6168652995100028116L;

	public PagingNavigator(String id, final AbstractPageableView<T> pageable) {
		super(id);

		this.pageable = pageable;

		// 跳转页面的文本框
		add(new TextField<Integer>("linktoPage", pageModel, Integer.class));

		// 跳转链接
		add(new AjaxSubmitLink("jump") {
			private static final long serialVersionUID = 3136048066516895496L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				int page = BlankUtils.isBlank(pageModel.getObject()) ? 1 : Integer.parseInt(pageModel.getObject().toString());
				page = page <= 0 ? 1 : page;
				page = page > pageable.getPageCount() ? pageable.getPageCount() : page;
				pageable.setCurrentPage(page - 1);
				pageModel.setObject(page);
				setResponsePage(getPage());
			}
		});
	}

	@Override
	protected void onBeforeRender() {
		if (get("first") == null) {

			add(newPagingNavigationLink("first", pageable, 0));
			add(newPagingNavigationIncrementLink("prev", pageable, -1));
			add(newPagingNavigationIncrementLink("next", pageable, 1));
			add(newPagingNavigationLink("last", pageable, -1));

			add(new Label("totalCount", new LoadableDetachableModel<Integer>() {
				private static final long serialVersionUID = 2520628160575459994L;

				@Override
				protected Integer load() {
					return pageable.getItemCount();
				}
			}));

			add(new Label("pageCount", new LoadableDetachableModel<Integer>() {
				private static final long serialVersionUID = -4176115838811415121L;

				@Override
				protected Integer load() {
					return pageable.getPageCount();
				}

			}));

			add(new Label("currentPage", new LoadableDetachableModel<Integer>() {
				private static final long serialVersionUID = 8021707638699052353L;

				@Override
				protected Integer load() {
					return pageable.getPageCount() == 0 ? 0 : pageable.getCurrentPage() + 1;
				}

			}));
		}

		super.onBeforeRender();
	}

	protected AbstractLink newPagingNavigationLink(String id, IPageable pageable, int pageNumber) {
		return new NavigationLink<T>(id, pageable, pageNumber, pageModel);
	}

	protected AbstractLink newPagingNavigationIncrementLink(String id, IPageable pageable, int increment) {
		return new NavigationIncrementLink<T>(id, pageable, increment, pageModel);
	}
}

/**
 * 上一页和下一页的连接
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 * @param <T>
 *            操作的实体类型
 */
class NavigationIncrementLink<T> extends PagingNavigationIncrementLink<T> {

	private IModel<Integer> pageModel;
	private static final long serialVersionUID = 4200227624230107781L;

	public NavigationIncrementLink(String id, IPageable pageable, int increment, IModel<Integer> pageModel) {
		super(id, pageable, increment);
		this.pageModel = pageModel;
	}

	@Override
	public void onClick() {
		super.onClick();
		pageModel.setObject(pageable.getCurrentPage() + 1);
	}
}

/**
 * <pre>
 * 
 * 首页和尾页链接
 * 
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 * @param <T>
 *            操作的实体类型
 */
class NavigationLink<T> extends PagingNavigationLink<T> {

	private IModel<Integer> pageModel;
	private static final long serialVersionUID = -7182337243925047403L;

	public NavigationLink(String id, IPageable pageable, int pageNumber, IModel<Integer> pageModel) {
		super(id, pageable, pageNumber);
		this.pageModel = pageModel;
	}

	@Override
	public void onClick() {
		super.onClick();
		pageModel.setObject(getPageNumber() + 1);
	}
}